package com.learn.training.multithread.chapter6.minchapter3.demo2;

import java.util.ArrayList;

public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        var q = new TaskQueue();
        var ts = new ArrayList<Thread>();
        for (int i = 0; i < 15; i++) {
            Thread t = new Thread(){
                public void run() {
                    while (true) {
                        try {
                            String s = q.getTask();
                            System.out.println(Thread.currentThread().getName() + "  execute task : " + s);
                        } catch (InterruptedException e) {
                            return;
                        }
                    }
                }
            };
            t.start();
            ts.add(t);
        }

        Thread add = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                //添加task
                String s= "t-" + Math.random();
                System.out.println(Thread.currentThread().getName() + "  add task : " + s);
                q.addTask(s);
                try {
                    Thread.sleep(100);
                }catch (InterruptedException e){}
            }
        });
        add.start();
        add.join();
        Thread.sleep(100);
        for (Thread t : ts) {
            t.interrupt();
        }

    }
}
